package main

import (
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"path"
	"regexp"
	"strconv"
	"strings"
	"studygo/other/picutils"
)

var (
	baseUrl     = "https://www.nvshens.org/"
	picSavePath = "d:/tmp/psnv/"
)

func main() {
	picutils.ExecuteCmd("curl", "--referer", baseUrl, "https://img.onvshen.com:85/gallery/24633/33345/s/0.jpg", "-o",
		"d:/tmp/psnv/33345_41/000.jpg")
	return

	picutils.MkdirFileAll("d:/pss", "gr/gre")
	sInt := fmt.Sprintf("%07d", 12)
	fmt.Println("字符串补齐0", sInt)
	var firstPicUrl = "https://img.onvshen.com:85/gallery/24633/33979/s/0.jpg"
	index := strings.LastIndex(firstPicUrl, "/")
	strBaseUrl := firstPicUrl[:index+1] //文件基本名字(除去最后扩展名)
	picExt := path.Ext(firstPicUrl)     //文件扩展名
	fmt.Println(index, strBaseUrl, picExt)
	return

	count := 41
	fmt.Println(((count + 29) / 30))
	for i := 1; i < ((count + 29) / 30); i++ {
		strurl := "album/" + strconv.Itoa(i) + ".html"
		fmt.Println(strurl)
	}
	sprintf := fmt.Sprintf("%s%s", 1, 2)
	fmt.Println(sprintf)
	var a string
	a = "123456"
	b, error := strconv.Atoi(a)
	if error != nil {
		fmt.Println("字符串转换成整数失败")
	}
	fmt.Println(b)
	str := "https://www.nvshens.org/g/34020/"
	split := strings.Split(str, "/")
	for i, s := range split {
		fmt.Println(i, s)
	}
	fmt.Println(split[len(split)-2])
}

// 解析 图片url(https://www.nvshens.org/g/34020/)
func parsePicByUrl(url string) {
	resp, err := http.Get(url)
	if err != nil {
		fmt.Println("解析图片地址失败", url)
	}
	doc, err := goquery.NewDocumentFromReader(resp.Body)
	text := doc.Find("#dinfo span").Text()
	attr, exists := doc.Find("#hgallery img").First().Attr("src")
	fmt.Println(attr, exists)

	i, b := picutils.GetInt(text)
	if !b {
		fmt.Println(i)

	}
}

//从字符中提取数字 匹配不到数字返回 true
func getInt(text string) (int, bool) {
	//正则表达式 \D 匹配非数字
	compile := regexp.MustCompile(`\D*(\d+)\D*`)
	//	查找所有的 //3	使用正则表达式的提取功能|
	match := compile.FindAllStringSubmatch(text, 1) // 返回的是 [][]string
	pageCount, err := strconv.Atoi(match[0][1])
	if err != nil {
		fmt.Println("图片数量匹配错误")
		return 0, true
	}
	return pageCount, false
}
